import { _decorator, math, PolygonCollider2D, Sprite, UITransform, Vec2 } from 'cc';
import { BaseProperty } from './BaseProperty';
import DataManager from '../Runtime/DataManager';
import { generateEllipsePoints, createUINode, getFrameAsset, createPlaceholder, destroyNode } from '../utils';
const { ccclass } = _decorator;
// 水果
@ccclass('FriutManager')
export class FriutManager extends BaseProperty {
    init(item1) {
        let tile = this.node
        const polygonCollider2D = tile.addComponent(PolygonCollider2D)
        polygonCollider2D.points = [...generateEllipsePoints()]
        polygonCollider2D.offset = new Vec2(0, -28)
        const shadow = createUINode("shadow")

        shadow.setParent(tile)
        shadow.setPosition(0, -30)
        shadow.setScale(3, 3)
        const shadowSprite = shadow.addComponent(Sprite)
        shadowSprite.spriteFrame = getFrameAsset(DataManager.Instance.mapAssets, '185.png').spriteFrame
        shadowSprite.color = new math.Color("#ffffff90")
        const shadowTransform = shadow.addComponent(UITransform)
        shadowTransform.setContentSize(44, 26)

        const fruitNode = createUINode("fruitNode")
        fruitNode.setParent(tile)
        fruitNode.setScale(2.5, 2.5)
        fruitNode.setPosition(0, -10)
        const fruitSprite = fruitNode.addComponent(Sprite)
        fruitSprite.sizeMode = Sprite.SizeMode.TRIMMED
        fruitSprite.spriteFrame = getFrameAsset(DataManager.Instance.mapAssets, `${item1}.png`).spriteFrame

    }
    getSendData() {
        let x = 0
        let y = 0
        let destroy = true
        let index=0
        if (this.node && this.node.isValid) {
            destroy = false
            x = Math.round(this.node.getPosition().x / 10) * 10// 位置
            y = Math.round(this.node.getPosition().x / 10) * 10
            index=this.node?.getSiblingIndex()
        }
        return {
            destroy,
            x,// 位置
            y,
            remainingTime: 10,
            index
        }
    }
    setSendData(data) {
        if (data.destroy) {
            if (this.node && this.node.isValid) {
                destroyNode(this.node)
                createPlaceholder(this.node)
            }
        }
    }
}


